﻿using Clancy.Interfaces;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;

namespace Clancy.Bl
{
    public class StandAloneOperationAdapter : IOperationAdapter
    {
        private const string c_SurveilanceConfigFile = "SurveilanceConfigStandAlone.xml";

        private readonly IRecordingManager m_RecordingManager;
        private readonly ISurveilanceLogic m_SurveilanceLogic;
        private readonly ISurveilanceStorage m_SurveilanceStorage;
        private readonly IVideoSourceFactory m_VideoSourceFactory;
        private readonly IVideoSourceProvider m_VideoSourceProvider;

        public StandAloneOperationAdapter(ISurveilanceLogic surveilanceLogic,
            ISurveilanceStorage surveilanceStorage,
            IVideoSourceProvider videoSourceProvider,
            IVideoSourceFactory videoSourceFactory,
            IRecordingManager recordingManager)
        {
            m_SurveilanceLogic = surveilanceLogic;
            m_SurveilanceStorage = surveilanceStorage;
            m_VideoSourceProvider = videoSourceProvider;
            m_VideoSourceFactory = videoSourceFactory;
            m_RecordingManager = recordingManager;

            m_SurveilanceLogic.LoadConfiguration(c_SurveilanceConfigFile);
            m_SurveilanceLogic.Start();
        }

        public ObservableCollection<IRecording> RecordingCollection
        {
            get { return m_RecordingManager.RecordingCollection; }
        }

        public Task AddNewSource(IVideoSourceFilterInfo videoSourceFilterInfo)
        {
            m_SurveilanceLogic.AddNewSource(videoSourceFilterInfo);
            return Task.FromResult(true);
        }

        public Task<bool> DeleteRecording(Guid selectedRecordingId)
        {
            return m_RecordingManager.DeleteRecording(selectedRecordingId);
        }

        public Task DeleteVideoSource(ISurveilanceObject selectedSurveilanceObject)
        {
            return m_SurveilanceLogic.DeleteSurveilance(selectedSurveilanceObject);
        }

        public void Dispose()
        {
            m_SurveilanceLogic.SaveConfiguration(c_SurveilanceConfigFile);
            m_SurveilanceLogic.Dispose();
        }

        public ISurveilanceObject[] GetAllSurveilances()
        {
            return m_SurveilanceStorage.SurveilanceObjects.ToArray();
        }

        public IVideoSourceFilterInfo[] GetAvailableSources()
        {
            return m_VideoSourceProvider.GetVideoSources().ToArray();
        }

        public ISurveilanceStatus GetSurveilance(ISurveilanceObject surveilanceObject)
        {
            return m_SurveilanceLogic.GetSurveilance(surveilanceObject);
        }

        public Task ShowAdvancedVideoDialog(ISurveilanceObject selectedSurveilanceObject)
        {
            IVideoSourceHandler videoSourceHandler = m_VideoSourceFactory.GetVideoSourceHandler(selectedSurveilanceObject);
            if (videoSourceHandler == null)
                return Task.FromResult(false);

            videoSourceHandler.ShowSettingsDialog();

            return Task.FromResult(true);
        }

        public Task StartVideoStream(ISurveilanceObject survObj)
        {
            return Task.FromResult(true);
        }

        public Task StopVideoStream(ISurveilanceObject survObj)
        {
            return Task.FromResult(true);
        }

        public Task UpdateSurveilanceObject(ISurveilanceObject surveilanceObject)
        {
            return Task.FromResult(true);
        }

        public Task UpdateSurveilanceObject(ISurveilanceObject selectedSurveilanceObject, string propertyName)
        {
            return Task.FromResult(true);
        }
    }
}